package com.lailai.exception;

import com.lailai.constant.ErrorCode;
import com.lailai.domainservice.response.SingleResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author lgr
 * @Date 2025/6/30
 * @Wechat a983105834
 * @Email 983105834@qq.com
 * @Copyright <a href="www.lailai.ink">lailai个人空间</a>
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(BusinessException.class)
    public SingleResponse<String> handleBusinessException(BusinessException ex) {
        logger.error("业务异常: code={}, message={}", ex.getCode(), ex.getMessage(), ex);
        return SingleResponse.ofFailure(String.valueOf(ex.getCode()), ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public SingleResponse<String> handleOtherException(Exception ex) {
        logger.error("系统异常", ex);
        return SingleResponse.ofFailure(ErrorCode.SYSTEM_ERROR.getCodeAsString(), "系统内部错误");
    }
}